package javaeSE;// Arrays工具类
import java.util.Arrays;

public class Array {
    public static void main(String[] args) {

        int[] arr = new int[]{8, 2, 1, 0, 3};
        // int[] copyarr = arr;  = 复制有瑕疵，会出现一个变其他的也变的情况
        //copyarr[0] =89899;
        int[] index = new int[]{2, 0, 3, 2, 4, 0, 1, 3, 2, 3, 3};
        String tel = "";
        for (int i = 0; i < index.length; i++) {
            tel += arr[index[i]];
        }
        System.out.println("联系方式：" + tel);//18...
        System.out.println("二维数组----------:");

        // Array.array2();
        System.out.println("=====反转与copy=======");
        Array.reverse();
        System.out.println("======二分查找======");
        int[] arrs = {10, 30, 45, 60, 79, 88, 100, 104, 200};
        int indexx = searchNumber(arrs, 0, arrs.length - 1, 88);
        if (indexx == -1) {
            System.out.println("在数组中没有找到该数字");
        } else {
            System.out.println("查找的数字在数组中的索引为" + indexx);
        }

        System.out.println("=======冒泡排序");
        Array.bubbleSort();
        System.out.println();
        System.out.println("=======Arrays工具类的使用");
        Array.arrayUtil();
    }

    public static void array2() {
        //二维数组初始化  了解
        int[][] arr = {{12, 58}, {56, 78, 41}, {11, 22, 33}};
        //System.out.println(arr[1][1]);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + "  ");
            }
            System.out.println();
        }
        //二维数组初始化
        int[][] data2 = new int[3][4];
        int sum = 0;
        for (int i = 0; i < data2.length; i++) {
            for (int j = 0; j < data2[i].length; j++) {
                data2[i][j] = (i + 1) * j;
                System.out.print(data2[i][j] + ",");
                sum += data2[i][j];
            }

            System.out.println();
        }
        System.out.println("和为:" + sum);
    }

    //  数组反转
    public static void reverse() {
        String[] arr = new String[]{"JJ", "DD", "MM", "BB", "GG", "AA"};
        //数组的复制(区别于数组变量的赋值：arr1 = arr)
        String[] arr1 = new String[arr.length];
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = arr[i];
        }
        System.out.println(arr1[0] + "," + arr1[1]);
        //数组的反转
        //方法一：
        for (int i = 0; i < arr.length / 2; i++) {
            String temp = arr[i];
            arr[i] = arr[arr.length - i - 1];
            arr[arr.length - i - 1] = temp;
        }
        System.out.println(arr[0] + "," + arr[1]);
    }

    //  数组的查找 与二分查找
    public static int searchNumber(int[] arr, int left, int right, int num) {
        int mid = (left + right) / 2;
        while (left <= right) {
            if (arr[mid] == num) {
                return mid;
            } else if (arr[mid] < num) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
            mid = (left + right) / 2;
        }
        return -1;
    }
    // 数组冒泡排序  冒泡排序每轮被决定的元素都会放在相对于最后一个(依次前推)然后不参与下次循环
    // 对象属性 进行排序的时候，，，比的是属性，换的是对象
    public static void bubbleSort(){
        int[] arr = new int[]{20, 0, 33, 21, 44, 10, 7, 5, 2, 1, 99};
        // i是控制 最后哪几个不用再比了
        for (int i = 0;i<arr.length;i++){
            // 第二层循环条件   j < arr.length 也可以
            for (int j = 0 ; j < arr.length - 1 - i;j++){
                if (arr[j]<arr[j+1]){
                 /*   int temp = arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1] = temp;*/
                    swap(arr,j,j+1);
                }
            }
        }
        for (int i = 0;i<arr.length;i++){
            if (i==arr.length-1){
                System.out.print(arr[i]);
            }else {
                System.out.print(arr[i]+">");
            }

        }
    }
    public static void swap(int[] arr,int i,int j){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;

    }
    // 快速排序

    // 工具类Arrays
    public static void arrayUtil(){
        //1.boolean equals(int[] a,int[] b):判断两个数组是否相等。
        int[] arr1 = new int[]{1,2,3,4};
        int[] arr2 = new int[]{1,3,2,4};
        // 顺序不同不算等
        // arr1.equals(arr2) 比较的是地址
        // 发现Arrays.equals()会比较两个数组中的每个元素的值是值 必须一一对应
        boolean isEquals = Arrays.equals(arr1, arr2);
        System.out.println(isEquals);
        //2.String toString(int[] a):输出数组信息。
        System.out.println(Arrays.toString(arr1));


        //4.void sort(int[] a):对数组进行排序。
        Arrays.sort(arr2);

        System.out.println(Arrays.toString(arr2));

        //5.int binarySearch(int[] a,int key)
        int[] arr3 = new int[]{-98,-34,2,34,54,66,79,105,210,333};
        // 二分查找
        int index = Arrays.binarySearch(arr3, 210);
        if(index >= 0){
            System.out.println(index);
        }else{
            System.out.println("未找到");
        }
    }
}
